package mrms.api.web.controllers;

import lombok.AllArgsConstructor;
import mrms.api.entity.SysconfEntity;
import mrms.api.repository.SysConfigRepository;
import mrms.api.web.utils.Result;
import org.springframework.web.bind.annotation.*;

import java.util.Optional;

/**
 * @author lenchu
 * @date 2019/02/16
 */
@RestController
@AllArgsConstructor
public class SysConfigController {
    private final SysConfigRepository sysConfigRepository;

    @GetMapping("/a/config/{k}")
    public Result configGet(@PathVariable String k) {
        Optional<SysconfEntity> optionalSysconfEntity = sysConfigRepository.findById(k);
        if (optionalSysconfEntity.isPresent()) {
            return Result.success(optionalSysconfEntity.get());
        } else {
            return Result.error("无此项配置");
        }
    }

    @PostMapping("/a/config")
    public Result configSet(@RequestBody SysconfEntity sysconf) {
        return Result.success(sysConfigRepository.save(sysconf));
    }

    @DeleteMapping("/a/config/{k}")
    public Result configDelete(@PathVariable String k) {
        Optional<SysconfEntity> optionalSysconfEntity = sysConfigRepository.findById(k);
        if (optionalSysconfEntity.isPresent()) {
            sysConfigRepository.deleteById(k);
            return Result.success(optionalSysconfEntity.get());
        } else {
            return Result.error("无此项配置");
        }
    }
}
